﻿using AutoMapper;
using MediatR;
using NPOI.XSSF.UserModel.Extensions;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Orderers
{
    /// <summary>
    /// 订单行
    /// </summary>
    public class OrderLinesCreateHandler : IRequestHandler<OrderLineCreateCommend, Result>
    {
        private readonly IBaseRepository<OrderLine> OrderLine;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="Resource"></param>
        public OrderLinesCreateHandler(IMapper mapper, IBaseRepository<OrderLine> OrderLine)
        {
            this.mapper = mapper;
            this.OrderLine = OrderLine;
        }

        public async Task<Result> Handle(OrderLineCreateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Added };

            var AddData = mapper.Map<OrderLine>(request);
            string sql = "insert into OrderLine values(@OrderLineId,@InventoryInfoId,@Prices,@Nums,@Unit,@Summary,@OrdersId,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = OrderLine.Add(sql, new
            {
                OrderLineId = AddData.OrderLineId,
                InventoryInfoId = AddData.InventoryInfoId,
                Prices = AddData.Prices,
                Nums = AddData.Nums,
                Unit = AddData.Unit,
                Summary = AddData.Summary,
                OrdersId= AddData.OrdersId,
                IsDelete = AddData.IsDelete,
                Handle = AddData.Handle = "李四",
                HandleContext = AddData.HandleContext = "添加",
                HandleTime = AddData.HandleTime = DateTime.Now,
                Pid = AddData.Pid = 0,
            });
            if (AddNum > 0)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "添加成功";
                return results;
            }
            else
            {
                return results;
            }
        }
    }
}
